﻿Imports System.Text

Namespace Global.System

    Public Module Extension

        <Extension()> _
        Public Function SplitLines(ByVal Source As String, Optional ByVal op As StringSplitOptions = StringSplitOptions.RemoveEmptyEntries) As String()
            Return SplitLines(Source, op)
        End Function

        <Extension()> _
        Public Function ChangeEncoding(ByVal [Me] As String, ByVal fromEncoding As Encoding, ByVal toEncoding As Encoding) As String
            Dim data = fromEncoding.GetBytes([Me])
            data = Encoding.Convert(fromEncoding, toEncoding, data)
            Return toEncoding.GetString(data, 0, data.Length)
        End Function

        <Extension()> _
        Public Function GetLeft(ByVal Source As String, ByVal Filter As String) As String
            If Source.Contains(Filter) = False Then
                Return ""
            Else
                Return Source.Substring(0, Source.IndexOf(Filter))
            End If
        End Function

        <Extension()> _
        Public Function GetRight(ByVal Source As String, ByVal Filter As String) As String
            If Source.Contains(Filter) = False Then
                Return ""
            Else
                Return Source.Substring(Source.IndexOf(Filter) + Filter.Length)
            End If
        End Function

        <Extension()> _
        Public Function Substring(ByVal [Me] As String, ByVal LeftChar As String, ByVal RightChar As String, Optional ByVal startIndex As Integer = 0) As String
            Dim posLeft = [Me].IndexOf(LeftChar, startIndex)
            Dim posRight = [Me].IndexOf(RightChar, posLeft + 1)
            Return [Me].Substring(posLeft + LeftChar.Length, posRight - posLeft - LeftChar.Length)
        End Function

        <Extension()> _
        Public Function RightOfLastChar(ByVal [Me] As String, ByVal lstChar As String) As String
            Dim pos = [Me].LastIndexOf(lstChar) + lstChar.Length
            Return [Me].Substring(pos)
        End Function

        <Extension()> _
        Public Function AllIndexOf(ByVal [Me] As String, ByVal filter As String) As List(Of Integer)
            Dim lst As New List(Of Integer)
            For i As Integer = 0 To [Me].Length - 1
                If [Me].Substring(i).StartsWith(filter) Then
                    lst.Add(i)
                End If
            Next
            Return lst
        End Function

#If NETFX_CORE Then

        <Extension()> _
        Public Function Take(ByVal [Me] As String, ByVal count As Integer) As IList(Of Char)
            Return [Me].Substring(0, count).ToCharArray()
        End Function

#End If

    End Module

End Namespace